package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.dao.InterestUserDao;
import com.qc.entity.InterestUserEntity;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("interestUserDao")
public class InterestUserDaoImpl implements InterestUserDao{

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<InterestUserEntity> listAllInterestUser(long userId, long receiveId, int pageNo, int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
		
		String sql = "select a.iu_id,"
				+ "	a.sender_user_id,"
				+ "	a.receive_user_id,"
				+ " a.modified_time,"
				+ " b.avatar,"
				+ " b.nickname,"
				+ " b.user_age,"
				+ " b.sex,"
				+ " c.sec_height,"
				+ " c.job,"
				+ " c.live_province,"
				+ " c.b_name"
				+ " from t_interest_user a ";
		
		if ( userId != 0) {
			sql += " left join t_user b on b.user_id = a.receive_user_id ";
			sql += " left join t_user_secretary c on c.user_id = b.user_id";
			sql += " where 1 = 1";
		    sql += " and a.sender_user_id = '"+userId+"' ";
		}

		if ( receiveId != 0) {
			sql += " left join t_user b on b.user_id = a.sender_user_id ";
			sql += " left join t_user_secretary c on c.user_id = b.user_id";
			sql += " where 1 = 1";
		    sql += " and a.receive_user_id = '"+receiveId+"' ";
		}
		
		sql += " order by a.modified_time DESC ";
		sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(InterestUserEntity.class));
	}

}
